from manager.comManager import *


class TabController(object):
    _instance_lock = threading.Lock()
    _first_init = True

    def __init__(self):
        if self._first_init:
            self.ui = None
            self._first_init = False
            self.bConnect = False
            self.str_send = "AT+CIPSEND" + "\r\n"
            self.str_not_send = "+++"
        pass

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance'):
            with TabController._instance_lock:
                if not hasattr(cls, '_instance'):
                    TabController._instance = super().__new__(cls)
        return TabController._instance

    def __del__(self):
        pass

    def init_ui(self, ui):
        self.ui = ui
        self.ui.cobox_controller_trans.addItems(['UDP', 'TCP'])
        self.ui.edit_controller_port.setText("2000")
        self.ui.edit_controller_port.setReadOnly(True)
        self.ui.edit_controller_addr.setText("192.168.99.2")
        self.ui.groupBox_change.setEnabled(False)
        self.ui.groupBox_control_watering.setEnabled(False)
        self.ui.groupBox_control_dust.setEnabled(False)
        self.init_connect()

    def init_connect(self):
        self.ui.cobox_controller_trans.currentTextChanged.connect(self.cobox_trans_changed)
        self.ui.btn_controller_connect.clicked.connect(self.btn_controller_connect)
        self.ui.btn_controller_debug.clicked.connect(self.btn_controller_debug)
        self.ui.btn_controller_quit.clicked.connect(self.btn_controller_quit)
        self.ui.btn_lift_up.clicked.connect(self.btn_lift_up_clicked)
        self.ui.btn_lift_down.clicked.connect(self.btn_lift_down_clicked)
        self.ui.btn_lift_center.clicked.connect(self.btn_lift_center_clicked)
        self.ui.btn_lift_stop.clicked.connect(self.btn_lift_stop_clicked)
        self.ui.btn_change_remove_pad.clicked.connect(self.btn_change_remove_pad_clicked)
        self.ui.btn_change_change_auto.clicked.connect(self.btn_change_change_auto_clicked)
        self.ui.btn_new_box_init.clicked.connect(self.btn_new_box_init_clicked)
        self.ui.btn_new_box_work.clicked.connect(self.btn_new_box_work_clicked)
        self.ui.btn_new_box_stop.clicked.connect(self.btn_new_box_stop_clicked)
        self.ui.btn_old_box_init.clicked.connect(self.btn_old_box_init_clicked)
        self.ui.btn_old_box_work.clicked.connect(self.btn_old_box_work_clicked)
        self.ui.btn_old_box_stop.clicked.connect(self.btn_old_box_stop_clicked)
        self.ui.btn_watering_debug.clicked.connect(self.btn_watering_debug_clicked)
        self.ui.btn_watering_quit_debug.clicked.connect(self.btn_watering_quit_debug)
        self.ui.btn_watering_watering.clicked.connect(self.btn_watering_watering_clicked)
        self.ui.btn_watering_pump.clicked.connect(self.btn_watering_pump_clicked)
        self.ui.btn_watering_stop.clicked.connect(self.btn_watering_stop_clicked)
        self.ui.btn_watering_old_box_init.clicked.connect(self.btn_watering_old_box_init_clicked)
        self.ui.btn_watering_old_box_work.clicked.connect(self.btn_watering_old_box_work_clicked)
        self.ui.btn_watering_old_box_stop.clicked.connect(self.btn_watering_old_box_stop_clicked)
        self.ui.btn_dust_debug.clicked.connect(self.btn_dust_debug_clicked)
        self.ui.btn_dust_quit_debug.clicked.connect(self.btn_dust_quit_debug_clicked)
        self.ui.btn_dust_auto.clicked.connect(self.btn_dust_auto_clicked)

    def isValid(self, ip, port):
        if ip == "":
            self.ui.statusBar.showMessage("No Tcp server IP input!!!")
            return False
        elif port == "":
            self.ui.statusBar.showMessage("No Tcp server port input!!!")
            return False
        else:
            return True

    def cobox_trans_changed(self):
        if self.ui.cobox_controller_trans.currentIndex() == 0:
            self.ui.label_40.setEnabled(True)
            self.ui.edit_controller_port.setEnabled(True)
        else:
            self.ui.label_40.setEnabled(False)
            self.ui.edit_controller_port.setEnabled(False)

    def connect_station(self):
        str_type = ""
        if self.ui.cobox_controller_trans.currentIndex() == 0:
            str_type = "\"UDP\""
        elif self.ui.cobox_controller_trans.currentIndex() == 1:
            str_type = "\"TCP\""
        str_at_connect = "AT+CIPSTART=" + str_type + "," \
                         + "\"" + self.ui.edit_controller_addr.text() + "\"" + "," \
                         + str(int(self.ui.edit_controller_port.text())) + "," \
                         "2000,0" + "\r\n"
        str_tranmit = "AT+CIPMODE=1" + "\r\n"
        comManager().send_data(SEND_TYPE.STRING_SEND, str_at_connect)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, str_tranmit)
        time.sleep(0.3)
        # comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 02 00 00')
        # time.sleep(0.3)
        # len_rev, c_info = comManager().receive_data(0)
        pass

    @staticmethod
    def disconnect_station():
        str_transmit = "AT+CIPMODE=0" + "\r\n"
        str_close = "AT+CIPCLOSE" + "\r\n"
        comManager().send_data(SEND_TYPE.STRING_SEND, str_transmit)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, str_close)
        time.sleep(0.5)
        pass

    def get_connect(self):
        return self.bConnect

    def btn_controller_connect(self):
        if self.bConnect:
            self.bConnect = False
            self.ui.btn_controller_connect.setText("连接")
            self.ui.groupBox_change.setEnabled(False)
            self.ui.groupBox_control_watering.setEnabled(False)
            self.ui.groupBox_control_dust.setEnabled(False)
            Common().set_ap_sta_connect(False)
            self.disconnect_station()
        else:
            self.bConnect = True
            self.ui.btn_controller_connect.setText("关闭")
            self.ui.groupBox_change.setEnabled(True)
            self.ui.groupBox_control_watering.setEnabled(True)
            self.ui.groupBox_control_dust.setEnabled(True)
            Common().set_ap_sta_connect(True)
            # self.ui.btn_controller_debug.setEnabled(True)
            # self.ui.btn_controller_quit.setEnabled(True)
            self.connect_station()
        pass

    def show_group_box(self, b: bool):
        self.ui.groupBox_control_lift.setEnabled(b)
        self.ui.groupBox_control_one.setEnabled(b)
        self.ui.groupBox_control_new_move.setEnabled(b)
        self.ui.groupBox_control_old_move.setEnabled(b)
        self.ui.groupBox_control_watering.setEnabled(b)
        self.ui.groupBox_control_dust.setEnabled(b)

    def btn_controller_debug(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 C8 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        pass

    def btn_controller_quit(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 C9 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        pass

    def btn_lift_up_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 D5 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_lift_down_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 D2 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_lift_center_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'FE')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, '03')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_lift_stop_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 D4 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_change_remove_pad_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 D3 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_change_change_auto_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 21 01 05 00')
        time.sleep(1)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 21 01 00 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_new_box_init_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 DC 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_new_box_work_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 DE 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_new_box_stop_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 DD 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_old_box_init_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 E6 00')
        time.sleep(0.3)
        # comManager().send_data(SEND_TYPE.HEX_SEND, 'FF')
        # time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_old_box_work_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 E8 00')
        time.sleep(0.3)
        # comManager().send_data(SEND_TYPE.HEX_SEND, 'FF')
        # time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_old_box_stop_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 21 E7 00')
        time.sleep(0.3)
        # comManager().send_data(SEND_TYPE.HEX_SEND, 'FF')
        # time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_watering_debug_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 31 C8 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        pass

    def btn_watering_quit_debug(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 31 C9 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        pass

    def btn_watering_old_box_work_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 31 E8 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        pass

    def btn_watering_old_box_stop_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 31 E7 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        pass

    def btn_watering_watering_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 31 D4 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_watering_pump_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 31 D2 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_watering_stop_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 31 D3 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

    def btn_watering_old_box_init_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 31 E6 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        pass

    def btn_dust_debug_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 41 C8 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        pass

    def btn_dust_quit_debug_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 41 C9 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)
        pass

    # stop after 10s
    def btn_dust_auto_clicked(self):
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_send)
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.HEX_SEND, 'A5 AA 02 41 CA 00')
        time.sleep(0.3)
        comManager().send_data(SEND_TYPE.STRING_SEND, self.str_not_send)

